using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using Module.Common.Models;
using Module.Core.EntitiesInfo;

namespace Module.Core.DAO
{
	public class LogMuaHangDAO
	{
		#region Fields

		protected string connectionStringName;

		#endregion		

		#region Methods

		/// <summary>
		/// Saves a record to the Log_MuaHang table.
		/// </summary>
		public virtual int Insert(LogMuaHangInfo logMuaHangInfo)
		{
			SqlParameter[] parameters = new SqlParameter[]
			{
				new SqlParameter("@CreateDate", logMuaHangInfo.CreateDate),
				new SqlParameter("@IP", logMuaHangInfo.IP),
				new SqlParameter("@email", logMuaHangInfo.Email)
			};

			return (int) SqlClientUtility.ExecuteScalar(connectionStringName, CommandType.StoredProcedure, "Log_MuaHang_Insert", parameters);
		}

		/// <summary>
		/// Updates a record in the Log_MuaHang table.
		/// </summary>
		public virtual void Update(LogMuaHangInfo logMuaHangInfo)
		{
			SqlParameter[] parameters = new SqlParameter[]
			{
				new SqlParameter("@LogId", logMuaHangInfo.LogId),
				new SqlParameter("@CreateDate", logMuaHangInfo.CreateDate),
				new SqlParameter("@IP", logMuaHangInfo.IP),
				new SqlParameter("@email", logMuaHangInfo.Email)
			};

			SqlClientUtility.ExecuteNonQuery(connectionStringName, CommandType.StoredProcedure, "Log_MuaHang_Update", parameters);
		}

		/// <summary>
		/// Deletes a record from the Log_MuaHang table by its primary key.
		/// </summary>
		public virtual void Delete(int logId)
		{
			SqlParameter[] parameters = new SqlParameter[]
			{
				new SqlParameter("@LogId", logId)
			};

			SqlClientUtility.ExecuteNonQuery(connectionStringName, CommandType.StoredProcedure, "Log_MuaHang_Delete", parameters);
		}

		/// <summary>
		/// Selects a single record from the Log_MuaHang table.
		/// </summary>
		public virtual LogMuaHangInfo Select(int logId)
		{
			SqlParameter[] parameters = new SqlParameter[]
			{
				new SqlParameter("@LogId", logId)
			};

			using (SqlDataReader dataReader = SqlClientUtility.ExecuteReader(connectionStringName, CommandType.StoredProcedure, "Log_MuaHang_Select", parameters))
			{
				if (dataReader.Read())
				{
					return MakeLogMuaHangInfo(dataReader);
				}
				else
				{
					return null;
				}
			}
		}

		/// <summary>
		/// Selects all records from the Log_MuaHang table.
		/// </summary>
		public CHRTList<LogMuaHangInfo> SelectAll()
		{
			using (SqlDataReader dataReader = SqlClientUtility.ExecuteReader(connectionStringName, CommandType.StoredProcedure, "Log_MuaHang_SelectAll"))
			{
				CHRTList<LogMuaHangInfo> logMuaHangInfoList = new CHRTList<LogMuaHangInfo>();
				while (dataReader.Read())
				{
					LogMuaHangInfo logMuaHangInfo = MakeLogMuaHangInfo(dataReader);
					logMuaHangInfoList.Add(logMuaHangInfo);
				}

				return logMuaHangInfoList;
			}
		}

        public CHRTList<LogMuaHangInfo> SearchAll(DateTime createdOnFrom, DateTime createdOnTo, int from, int to)
        {
            SqlParameter[] parameters = new SqlParameter[]
                                          {
                                           new SqlParameter("@CreatedOnFrom",createdOnFrom),
                                           new SqlParameter("@CreatedOnTo ",createdOnTo),
                                           new SqlParameter("@From ",from),
                                           new SqlParameter("@To ",to)

		};
            using (SqlDataReader dataReader = SqlClientUtility.ExecuteReader(connectionStringName, CommandType.StoredProcedure, "Log_MuaHang_SearchAll", parameters))
            {
                CHRTList<LogMuaHangInfo> LogMuaHangInfoList = new CHRTList<LogMuaHangInfo>();
                while (dataReader.Read())
                {
                    LogMuaHangInfo logMuaHangInfo = MakeLogMuaHangInfo(dataReader);
                    LogMuaHangInfoList.Add(logMuaHangInfo);
                }

                return LogMuaHangInfoList;
            }
        }
        public int GetCount(int logId,DateTime createOnFrom, DateTime createOnTo)
        {
            SqlParameter[] parameters = new SqlParameter[]
                                            { 
                                                new SqlParameter("@LogId", logId), 
                                                new SqlParameter("@CreatedOnFrom", createOnFrom),
                                                new SqlParameter("@CreatedOnTo", createOnTo)
                                                
                                            };
            return
                (int)SqlClientUtility.ExecuteScalar(connectionStringName, CommandType.StoredProcedure, "Log_MuaHang_GetCount", parameters);

        }

        public virtual int SelectCount(bool isTotalRow)
        {
            SqlParameter[] parameters = new SqlParameter[]
			{           
				new SqlParameter("@isTotalRow", true.ToString())
			};

            return (int)SqlClientUtility.ExecuteScalar(connectionStringName, CommandType.StoredProcedure, "Sp_LogMuaHang_Search", parameters);
        }

        public virtual DataTable Search(int beginRow, int endRow, string orderByColumn)
        {
            SqlParameter[] parameters = new SqlParameter[]
			{
                new SqlParameter("@BeginRow", beginRow),
                new SqlParameter("@EndRow", endRow),
                new SqlParameter("@OrderByColumn", orderByColumn),
				new SqlParameter("@isTotalRow", false.ToString())
			};

            return SqlClientUtility.ExecuteDataTable(connectionStringName, CommandType.StoredProcedure, "Sp_LogMuaHang_Search", parameters);
        }

        public DataSet ListLogMuaHangPage(int logId, DateTime createOnFrom, DateTime createOnTo, int currPage, int recodperPage, int pageSize, int total, string link)
        {
            SqlParameter[] parameters = new SqlParameter[]
                                          {
                                              new SqlParameter("@LogId", logId),
                                              new SqlParameter("@CreatedOnFrom",createOnFrom),
                                              new SqlParameter("@CreatedOnTo",createOnTo),
                                              new SqlParameter("@currPage", currPage),
                                              new SqlParameter("@recodperPage", recodperPage),
                                              new SqlParameter("@pageSize", pageSize), 
                                              new SqlParameter("@total", total),
                                              new SqlParameter("@link", link)
                                          };
            using (DataSet ds = SqlClientUtility.ExecuteDataSet(connectionStringName, CommandType.StoredProcedure, "Log_MuaHang_search_Page", parameters))
            {
                if (ds.Tables[0].Rows.Count > 0)
                    return ds;
                return null;
            }
        }


		/// <summary>
		/// Creates a new instance of the Log_MuaHang class and populates it with data from the specified SqlDataReader.
		/// </summary>
		protected virtual LogMuaHangInfo MakeLogMuaHangInfo(SqlDataReader dataReader)
		{
			LogMuaHangInfo logMuaHangInfo = new LogMuaHangInfo();
			logMuaHangInfo.LogId = SqlClientUtility.GetInt32(dataReader,DbConstants.LOG_MUAHANG.LOGID, 0);
			logMuaHangInfo.CreateDate = SqlClientUtility.GetDateTime(dataReader,DbConstants.LOG_MUAHANG.CREATEDATE, DateTime.Now);
			logMuaHangInfo.IP = SqlClientUtility.GetString(dataReader,DbConstants.LOG_MUAHANG.IP, String.Empty);
			logMuaHangInfo.Email = SqlClientUtility.GetString(dataReader,DbConstants.LOG_MUAHANG.EMAIL, String.Empty);

			return logMuaHangInfo;
		}

		#endregion
	}
}
